$(function(){
      edit_board = function(old_board,new_board,selector) {
        $.ajax({
          //编辑board
          url:"/board/rename/"+old_board,type:"PUT",
          data:{new_board_name:new_board},dataType:"json",
          success:function(data){
            if (data["is_rename_ok"]) {
              selector.siblings(".board-link").attr("href","/board/"+new_board).html(new_board).css("display","inline");
              selector.siblings('.board-edit').removeAttr("style");
              selector.remove();
            } else {
              selector.siblings(".board-link").css("display","inline");
              selector.siblings('.board-edit').removeAttr("style");
              selector.remove();
              alert("编辑未完成");
            };
          },
          error:function(data){
            selector.siblings(".board-link").css("display","inline");
            selector.siblings('.board-edit').removeAttr("style");
            selector.remove();
            alert("网络错误");
          }
        });
      };
      delete_board = function(board_name,selector) {
        $.ajax({
          //删除board
          url:"/board/delete/"+board_name,type:"DELETE",
          data:{},dataType:"json",
          success:function(data){
            if (data["is_delete_ok"]) {
              selector.remove();
            } else {
              alert("删除失败");
            };
          },
          error:function(data){
            alert("网络错误");
          }
        });
      };
      bind_board = function(board_name) {
        $('<li class="board-item"><a class="board-link" href="/board/'+board_name+'">'+board_name+'</a><a class="board-delete">-删除</a><a class="board-edit">*编辑</a></li>').appendTo("#boards-body").children(".board-edit").on("click", function(){
          $(this).css("display","none");
          board_name = $(this).siblings(".board-link").text();
          window.global_board = board_name;
          $(this).siblings(".board-link").css("display","none");
          $(this).parent().prepend('<input type="text" class="board-editipt"/>');
          $(this).siblings('.board-editipt').val(board_name).select().on("keyup", function(e){
            if (e.keyCode != 13) return;
            if (!$(this).val()) return;
            new_board = $(this).val();
            edit_board(global_board,new_board,$(this));
            });
        }).siblings('.board-delete').on("click", function(){
          board_name = $(this).siblings(".board-link").text();
          if (confirm("确定删除 "+board_name+" ？")) {
            delete_board(board_name,$(this).parent());
          };
        });
      };
      init_board = function() {
        $.ajax({
          //初始化，获取boards数据
          url:"/board",type:"GET",cache:false,
          data:{get:"all"},dataType:"json",
          success:function(data){
            $.each(data, function(i,val) {
              bind_board(val);
            });
          },
          error:function(data){
            alert("网络错误");
          }
        });
      };
      add_board = function(board_name,selector) {
        $.ajax({
          //添加board
          url:"/board/create",type:"POST",
          data:{board_name:board_name},dataType:"json",
          success:function(data){
            if (data["is_create_ok"]) {
              bind_board(board_name);
              selector.remove();
            } else {
              alert("添加出错");
            };
          },
          error:function(data){
            alert("网络错误");
          }
        });
      };
      init_board();
      $("#board-add").on("click", function(){
        $('<input type="text" class="board-addipt"/>').appendTo("#boards-body").on("keyup", function(e){
          if (e.keyCode != 13) return;
          if (!$(this).val()) {$(this).remove();return;}
          board_name = $(this).val();
          add_board(board_name,$(this));
          });
      });
})